package cn.gdcp.jsonthree;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "ss";
    private Spinner spinner_sheng;
    private Spinner spinner_shi;
    private Spinner spinner_qu;
    private Map<String, String[]> cityMap = new HashMap<String, String[]>();//key:省p---value:市n  value是一个集合
    private Map<String, String[]> areaMap = new HashMap<String, String[]>();//key:市n---value:区s    区也是一个集合
    private String city;
    ArrayAdapter<String> provinceAdapter = null;  //省级适配器
    ArrayAdapter<String> cityAdapter = null;    //地级适配器
    ArrayAdapter<String> countyAdapter = null;    //县级适配器
    private String [] Pro;
    private int position = 0;
    private String[] CityArr;
    private String[] AcountArr;
    private String name1;
    private String name2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initBundler();
        initData();
        initSpinner();
    }

    private void initSpinner() {
        provinceAdapter=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,Pro);
        spinner_sheng.setAdapter(provinceAdapter);
        spinner_sheng.setSelection(0,true);
             name1=Pro[0];
            CityArr = cityMap.get("北京市");
            Log.e(TAG, "initSpinner: "+CityArr );
            cityAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,CityArr);
            spinner_shi.setAdapter(cityAdapter);
            spinner_shi.setSelection(0,true);
         name2=CityArr[0];
        AcountArr =areaMap.get(name2);
        countyAdapter=new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_dropdown_item_1line,AcountArr);
        spinner_qu.setAdapter(countyAdapter);
        spinner_qu.setSelection(0,true);
       spinner_sheng.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
           @Override
           public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
               String value= (String) spinner_sheng.getSelectedItem();
               CityArr=cityMap.get(value);
               cityAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,CityArr);
               spinner_shi.setAdapter(cityAdapter);

               name2=CityArr[0];
               AcountArr =areaMap.get(name2);
               countyAdapter=new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_dropdown_item_1line,AcountArr);
               spinner_qu.setAdapter(countyAdapter);

           }

           @Override
           public void onNothingSelected(AdapterView<?> adapterView) {

           }
       });

       spinner_shi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
           @Override
           public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

               String value= (String) spinner_shi.getSelectedItem();
               name2=value;
               AcountArr =areaMap.get(name2);
               countyAdapter=new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_dropdown_item_1line,AcountArr);
               spinner_qu.setAdapter(countyAdapter);
           }
           @Override
           public void onNothingSelected(AdapterView<?> adapterView) {

           }
       });

    }
    private void initBundler() {

        spinner_sheng = (Spinner) findViewById(R.id.spinner_sheng);
        spinner_shi = (Spinner) findViewById(R.id.spinner_shi);
        spinner_qu = (Spinner) findViewById(R.id.spinner_qu);

    }

    private void initData() {
        try {
            InputStream open = getResources().getAssets().open("data.txt");
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(open));
            StringBuffer stringBuffer=new StringBuffer();

            String next=bufferedReader.readLine();
            while (next!=null){
                stringBuffer.append(next);
                next=bufferedReader.readLine();
            }
            String txt=stringBuffer.toString();


            //JSON数组，获取全国所有省市区信息
            JSONArray jsonArray=new JSONArray(txt);

            Pro=new String[jsonArray.length()];


            for (int i=0;i<jsonArray.length();i++){

                JSONObject jsonObjectP=jsonArray.getJSONObject(i);
                String pro=jsonObjectP.getString("name");

/*
                Log.e(TAG, "省名："+pro );
*/
                Pro[i]=pro;

                JSONArray jsonArrayCity=jsonObjectP.getJSONArray("city");

              /*  Log.e(TAG, "每个省的所有城市: "+jsonArrayCity.toString() );*/

                String [] City=new String[jsonArrayCity.length()];
/*
                Log.e(TAG, "城市长度："+City.length);
*/

                for (int c=0;c<jsonArrayCity.length();c++) {

                    JSONObject jsonObjectCity = jsonArrayCity.getJSONObject(c);

                    /*Log.e(TAG, "jsonObjectCity："+jsonObjectCity );*/

                     city = jsonObjectCity.getString("name");
                  /*  Log.e(TAG, "每个JSONObject里的城市名："+city );*/

                    City[c] = city;

                    JSONArray jsonArrayArea = jsonObjectCity.getJSONArray("area");
                   /* Log.e(TAG, "jsonArrayArea: "+ jsonArrayArea);*/

                    String[] Area = new String[jsonArrayArea.length()];

                    for (int a=0;a<jsonArrayArea.length();a++){
                        Area[a]=jsonArrayArea.getString(a);
                       /* Log.e(TAG, "Area："+ jsonArrayArea.getString(a));*/
                    }
                    areaMap.put(city,Area);
                }


                cityMap.put(pro,City);

            }
            Log.e(TAG, "cityMap："+cityMap);
            Log.e(TAG, "areaMap："+areaMap );


        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}
